package model;

import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class Group_Dao {
	
	private static ResultSet rs;
	public Chat_Group chat_group;
	public final static String  TABLE_NAME= "`group`";
	static PreparedStatement ps  ;
    public static Connection con = BaseConnection.getConnection();
    
    public Chat_Group getChat_group() {
		return chat_group;
	}
	public void setChat_group(Chat_Group chat_group) {
		this.chat_group = chat_group;
	}
	 
    
    public static void modify_NameById(String name,int group_id) {    //修改群名字
		String sql = "updata "+TABLE_NAME+" set name = '"+name+"' where group_id = '"+group_id+"'";
		 try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		
	}
	
	public static void modify_AvatarById(Blob avatar,int group_id) {    //修改群图片
		String sql = "updata "+TABLE_NAME+" set avatar = ? where group_id = '"+group_id+"'";
		 try {
			ps = con.prepareStatement(sql);
			ps.setBlob(1, avatar);
			ps.executeUpdate();
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		
	}
	
	public static void modify_Avatar_typeById(String avatar_type,int group_id) {  //修改群头像图片类型
		String sql = "updata "+TABLE_NAME+" set avatar_type = '"+avatar_type+"' where group_id = '"+group_id+"'";
		 try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
	}
	
	public static void modify_Maximum_numberById(int  maximum_number,int group_id) {//修改群最大人数
		String sql = "updata "+TABLE_NAME+" set maximum_number = '"+maximum_number+"' where group_id = '"+group_id+"'";
		 try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		
	}
	
	public static void modify_NatureById(String  nature,int group_id) {      //修改群类型
		String sql = "updata "+TABLE_NAME+" set maximum_number = '"+nature+"' where group_id = '"+group_id+"'";
		 try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		
	}

	public static String search_NameById(int group_id) {    //获取群名字
		 ResultSet r = searchById(group_id);
		 String temp = null;
		 try {
			 r.next();
			 temp = r.getString(2);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	
	public static String search_Avatar_type(int group_id) { //获取群头像图片类型
		 ResultSet r = searchById(group_id);
		 String temp = null;
		 try {
			 r.next();
			 temp = r.getString(3);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	public static Blob search_AvatarById(int group_id) {    //获取群头像
		 ResultSet r = searchById(group_id);
		 Blob temp = null;
		 try {
			 r.next();
			 temp = r.getBlob(4);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	
	public static Timestamp search_Creation_timeById(int group_id) {    //获取群创建时间
		 ResultSet r = searchById(group_id);
		 Timestamp temp = null;
		 try {
			 r.next();
			 temp = r.getTimestamp(7);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	
	public static int search_Maximum_numberById(int group_id) {    //获取群 最大人数
		 ResultSet r = searchById(group_id);
		 int temp = -1;
		 try {
			 r.next();
			 temp = r.getInt(5);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	
	public static String search_NatureById(int group_id) {    //获取群性质
		 ResultSet r = searchById(group_id);
		 String temp = null;
		 try {
			 r.next();
			 temp = r.getString(6);
		 }catch(SQLException e) {
			 e.printStackTrace();
		 }
		return temp;
	}
	
	public static ResultSet searchById(int group_id) {							//查看群信息(用做调用)
		String sql = "selec   from "+TABLE_NAME+" where id ='"+group_id+"'";
//		Connection con = BaseConnection.getConnection();
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return rs;
		
	}
	
	public void  deleteGroupById(int group_id) {						//删除某个群聊
		String sql = "delete  from "+TABLE_NAME+" where group_id = '"+group_id+"'";
		try{
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void addGroup(String name,String avatar_type,Blob avatar,int number,String nature,Timestamp time) {
		this.chat_group = new Chat_Group();
		chat_group.setGroup_name(name);
		chat_group.setGroup_avater_type(avatar_type);
		chat_group.setGour_max_number(number);
		chat_group.setGroup_avatar(avatar);
		chat_group.setGroup_nature(nature);
		chat_group.setGroup_create_time(time);
		addGroup();
	}
	public void addGroup() {				//创建群聊
		String sql = "insert into"+TABLE_NAME+"(name,avatar_type,avatar,maximum_number,nature,creation_time) values('"+chat_group.getGroup_name()+"','"+chat_group.getGroup_avater_type()+"',?,'"+chat_group.getGour_max_number()+"','"+chat_group.getGroup_nature()+"','"+chat_group.getGroup_create_time()+"')";
		try {
			ps = con.prepareStatement(sql);
			ps.setBlob(1,chat_group.getGroup_avatar());
			ps.execute();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	public  void update()  { 						//更新数据库(通过传入一个Chat_Group对象
		String sql = "update "+TABLE_NAME+" set name='"+chat_group.getGroup_name()+"', avatar=?,maximum_number='"+ chat_group.getGour_max_number()+"',nature='"+chat_group.getGroup_nature()+"' where id ='"+chat_group.getGroup_id()+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.setBlob(1,chat_group.getGroup_avatar());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static ResultSet searchGroup(int userId){
        String sql = "select  g.name ,g.avatar_type,g.avatar from `group` g,group_member m "+
                "where g.id=m.group_id AND m.user_id='"+userId+"'AND g.nature NOT IN('好友群')";
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
           rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
       return  rs;
    }
	
}
